package zj.xinxin.business.business.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import zj.xinxin.api.moudle.security.api.RoleService;
import zj.xinxin.api.moudle.security.dto.ConsumerDetailDto;
import zj.xinxin.api.moudle.security.dto.RoleDto;
import zj.xinxin.api.moudle.security.entity.RoleEntity;
import zj.xinxin.api.moudle.security.vo.RoleCreateVo;
import zj.xinxin.api.moudle.security.vo.RoleInfoSearchVo;
import zj.xinxin.api.moudle.security.vo.RoleUpdateVo;
import zj.xinxin.business.business.security.converter.RoleConverter;
import zj.xinxin.mapper.mapper.security.RoleEntityMapper;
import zj.xinxin.api.moudle.utils.ConsumerUtil;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author junzhou
 * @date 2022/3/23 17:39
 * @since 1.8
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleEntityMapper, RoleEntity> implements RoleService {

    @Override
    public List<RoleDto> getRoleList() {

        Integer currentRoleId = getCurrentUserRoleId();

        if (currentRoleId == null) {
            return Collections.emptyList();
        }

        List<RoleDto> result = this.list(new LambdaQueryWrapper<RoleEntity>()
                        .ge(currentRoleId != 0, RoleEntity::getRoleId, currentRoleId))
                .stream()
                .map(RoleConverter.INSTANCE::toDto)
                .collect(Collectors.toList());
        return result.isEmpty() ? Collections.emptyList() : result;
    }

    private Integer getCurrentUserRoleId() {
        ConsumerDetailDto userDetails = ConsumerUtil.getUserDetails();

        Optional<Integer> min = userDetails.getRoleList().stream()
                .map(RoleDto::getRoleId)
                .min(Comparator.comparing(Function.identity()));
        return min.orElse(-1);
    }

    @Override
    public List<RoleDto> searchByCondition(RoleInfoSearchVo searchVo) {

        Integer currentRoleId = getCurrentUserRoleId();
        if (currentRoleId == null) {
            return Collections.emptyList();
        }

        List<RoleDto> result = this.list(new LambdaQueryWrapper<RoleEntity>()
                        .ge(currentRoleId != 0, RoleEntity::getRoleId, currentRoleId)
                        .eq(searchVo.getIsDisable() != null,
                                RoleEntity::getIsDisable, searchVo.getIsDisable())
                        .and(StringUtils.isNotEmpty(searchVo.getName()),
                                wrapper ->
                                        wrapper.like(RoleEntity::getRoleName, searchVo.getName())
                                                .or()
                                                .like(RoleEntity::getRoleKey, searchVo.getName())
                        ))
                .stream()
                .map(RoleConverter.INSTANCE::toDto)
                .collect(Collectors.toList());
        return result.isEmpty() ? Collections.emptyList() : result;
    }


    @Override
    public Boolean addRole(RoleCreateVo roleInfo) {
        RoleEntity roleEntity = RoleConverter.INSTANCE.toEntity(roleInfo);
        boolean result = this.save(roleEntity);
        if (!result || roleEntity.getRoleId() == null){
            return Boolean.FALSE;
        }
        return true;
    }

    @Override
    public Boolean updateRole(RoleUpdateVo roleInfo) {
        RoleEntity roleEntity = RoleConverter.INSTANCE.toEntity(roleInfo);
        boolean result = this.updateById(roleEntity);
        if (!result || roleEntity.getRoleId() == null){
            return Boolean.FALSE;
        }
        return true;
    }

    @Override
    public RoleDto getConsumerRole() {

        RoleEntity roleInfo = this.getById(3);

        return RoleConverter.INSTANCE.toDto(roleInfo);
    }
}
